package com.daxt.controller.warehouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.warehouse.WarehouseShelfMapper;
import com.daxt.mapper.warehouse.WarehouseShelfPostionMapper;
import com.daxt.model.service.warehouse.param.SortWarehouse;
import com.daxt.model.service.warehouse.vo.WarehouseShelf;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostion;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "货架")
@RestController
@RequestMapping("/warehouse/shelf")
public class WarehouseShelfController {
	
	@Autowired 
	private WarehouseShelfMapper warehouseShelfMapper;
	@Autowired
	private WarehouseShelfPostionMapper warehouseShelfPostionMapper;
	@ApiOperation(value = "货架列表", notes = "") 
	@GetMapping("/list/{id}") 
	public Result<Map<String,List<WarehouseShelf>>> list(@PathVariable("id")String id){
		QueryWrapper<WarehouseShelf> queryWrapper=new QueryWrapper<WarehouseShelf>();
		queryWrapper.eq("wId", id);
		List<WarehouseShelf> list = warehouseShelfMapper.selectList(queryWrapper);
		Map<String,List<WarehouseShelf>> group=new HashMap<>();
		for (WarehouseShelf warehouseShelf : list) {
			if(group.containsKey(warehouseShelf.getSname())) {
				List<WarehouseShelf> tempList = group.get(warehouseShelf.getSname());
				if(tempList!=null&&tempList.size()>0) {
					tempList.add(warehouseShelf);
				}else {
					tempList=new ArrayList<WarehouseShelf>();
					tempList.add(warehouseShelf);
				}
				group.put(warehouseShelf.getSname(),tempList);
			}else {
				List<WarehouseShelf> tempList=new ArrayList<WarehouseShelf>();
				tempList.add(warehouseShelf);
				group.put(warehouseShelf.getSname(),tempList);
			}
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, group);
	}
	
	@ApiOperation(value = "货架列表all", notes = "") 
	@GetMapping("/listAll/{id}") 
	public Result<List<WarehouseShelf>> listAll(@PathVariable("id")String id){
		QueryWrapper<WarehouseShelf> queryWrapper=new QueryWrapper<WarehouseShelf>();
		queryWrapper.eq("wId", id);
		List<WarehouseShelf> list = warehouseShelfMapper.selectList(queryWrapper);
		
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "货架添加", notes = "") 
	@PostMapping("") 
	public Result<String> save(@RequestBody WarehouseShelf entity){
		QueryWrapper<WarehouseShelf> queryWrapper=new QueryWrapper<WarehouseShelf>();
		queryWrapper.eq("sname", entity.getSname());
		queryWrapper.eq("position",entity.getPosition());
		queryWrapper.eq("wId",entity.getWId());
		List<WarehouseShelf> selectList = warehouseShelfMapper.selectList(queryWrapper);
		if(selectList!=null&&selectList.size()>0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "该位置货架已添加", null);
		}
		warehouseShelfMapper.insert(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "货架修改", notes = "") 
	@PutMapping("") 
	public Result<String> update(@RequestBody WarehouseShelf entity){
		QueryWrapper<WarehouseShelf> queryWrapper=new QueryWrapper<WarehouseShelf>();
		queryWrapper.eq("sname", entity.getSname());
		queryWrapper.eq("position",entity.getPosition());
		queryWrapper.eq("wId",entity.getWId());
		List<WarehouseShelf> selectList = warehouseShelfMapper.selectList(queryWrapper);
		if(selectList!=null&&selectList.size()>0) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "该位置货架已添加", null);
		}
		warehouseShelfMapper.updateById(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "排序", notes = "") 
	@PutMapping("/sort") 
	@Transactional(rollbackFor = Exception.class)
	public Result<String> sort(@RequestBody List<SortWarehouse> entity){
		try {
		if(entity!=null&&entity.size()>0) {
			for (SortWarehouse sortWarehouse : entity) {
				WarehouseShelf warehouse=new WarehouseShelf();
				warehouse.setId(sortWarehouse.getId());
				warehouse.setSort(sortWarehouse.getSort());
				warehouseShelfMapper.updateById(warehouse);
			}
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
		}
		}catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作异常！！！", null);
		}
	}
	
	@ApiOperation(value = "货架删除", notes = "") 
	@DeleteMapping("") 
	public Result<List<String>> del(@RequestBody List<String> ids){
		if(ids!=null&&ids.size()>0) {
			List<String> cIds=new ArrayList<String>();
			for (String string : ids) {
				QueryWrapper<WarehouseShelfPostion> queryWrapper=new QueryWrapper<>();
				queryWrapper.eq("sId", string);
				List<WarehouseShelfPostion> list = warehouseShelfPostionMapper.selectList(queryWrapper);
				if(list!=null&&list.size()>0) {
					cIds.add(string);
				}
			}
			if(cIds.size()>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR_IDS.getCode(), "", cIds);
			}
			warehouseShelfMapper.deleteBatchIds(ids);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "删除至少选择一个货架！！！", null);
		}
	}
	
	
	
}
